package com.smx.steppingstone.renderer;

import javax.xml.xpath.XPathExpressionException;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.smx.steppingstone.util.XMLUtil;

public class HtmlRenderer extends AbstractRenderer{
	@Override
	protected  void emitObject(Node node) throws Exception {		
		c("<table class='class'>");
		c("<tbody>");
		c("<tr>");
		c("<td valign='top'>");
		
		if(node.getAttributes().getNamedItem("class")==null){
		c("<h1>NULL</h1>");
		}else{
			c("<h1>"+node.getAttributes().getNamedItem("class").getNodeValue()+"</h1>");
		}
		
		c("</td>");
		c("<td>");
		emitFields(XMLUtil.extract("field", XMLUtil.extract("fields", node).item(0)), false, false);
		
		NodeList collectionList = XMLUtil.extract("collection", XMLUtil.extract("fields", node).item(0));
		emitCollectionFields(collectionList, node);
			
		emitMethods(XMLUtil.extract("method", XMLUtil.extract("methods", node).item(0)));
		c("</td>");
		c("</tr>");
		c("</tbody>");
		c("</table>");
		c("\n");
	}
	
	@Override
	protected  void emitArrayFields(NodeList nodeList, Node owner)	throws Exception {
		c("<table class='collections'>");
		c("<thead>");
		c("<tr class='collections-header'  onclick='toggleView(this);'><th colspan='2'><h2>Arrays</h2></th></tr>");
		c("</thead>");
		c("<tbody>");
		for(int k=0;k<nodeList.getLength();k++){
			Node selectedNode=nodeList.item(k);
		c("<tr>");
		c("<td>");
		
		c("<tr>");
		c("<td>");
		
		c("<h2 class='black'>" 
		+ selectedNode.getAttributes().getNamedItem("type").getNodeValue()+"  " 
		+ selectedNode.getAttributes().getNamedItem("field").getNodeValue()
		+ "</h2>");
	
		c("</td>");
		c("</tr>");
		
		NodeList collectionObjects = XMLUtil.extract("./object", selectedNode);
		for(int j=0;j<collectionObjects.getLength();j++){
			NodeList collectionListSet = XMLUtil.extract("./row", collectionObjects.item(j));	
		if (collectionListSet.getLength() > 0){
			NodeList entrySet = XMLUtil.extract("./entry", collectionListSet.item(0));
			
			c("<tr style='width:100%'>");
			c("<td>  </td>");
			c("<td>");
				c("<table class='collections' style='width:100%'>");
				c("<thead>");
				c("<tr class='collections-header'  onclick='toggleView(this);'><th  colspan='2'><h2> Array </h2></th></tr>");
				c("</thead>");
				
			for (int i = 0; i < entrySet.getLength(); i++) {
				Node entry=entrySet.item(i);
				//	Node key= XMLUtil.extract("./key", entry).item(0);
				Node value= XMLUtil.extract("./index", entry).item(0);				
					c("<td valign='top' class='strong'> ");
						c(i);
					c("</td>");
					c("<td>");
						emit(value);
					c("</td>");
											
				c("</td>");
				c("</tr>");
			}						
			c("</tbody>");
			c("</table>");
			c("\n");
		}else{
			//for (int i = 0; i < nodeList.getLength(); i++) {
				c("<tr>");
				c("<td>  </td>");
				c("<td>");
					emit(selectedNode);
				c("</td>");
				c("</tr>");
			//}		
		}
		
		c("</tr>");
		c("</td>");
		}//for
		
		}//for
		c("</tbody>");
		c("</table>");
		c("\n");
				
	}
	
	@Override
	protected  void emitCollectionFields(NodeList nodeList, Node owner)	throws Exception {
		if(nodeList.getLength()==0)return;
		c("<table class='collections'>");
		c("<thead>");
		c("<tr class='collections-header'  onclick='toggleView(this);'><th colspan='2'><h2>Collections</h2></th></tr>");
		c("</thead>");
		c("<tbody>");
		for(int k=0;k<nodeList.getLength();k++){
			Node selectedNode=nodeList.item(k);
		c("<tr>");
		c("<td>");
		
		c("<tr>");
		c("<td>");
		
		c("<h2 class='black'>" 
		+ selectedNode.getAttributes().getNamedItem("type").getNodeValue()+"  " 
		+ selectedNode.getAttributes().getNamedItem("field").getNodeValue()
		+ "</h2>");
	
		c("</td>");
		c("</tr>");
		
		NodeList collectionObjects = XMLUtil.extract("./object", selectedNode);
		for(int j=0;j<collectionObjects.getLength();j++){
			NodeList collectionListSet = XMLUtil.extract("./set", collectionObjects.item(j));	
		if (collectionListSet.getLength() > 0){
			NodeList entrySet = XMLUtil.extract("./entry", collectionListSet.item(0));
			
			c("<tr style='width:100%'>");
			c("<td>  </td>");
			c("<td>");
				c("<table class='methods' style='width:100%'>");
				c("<thead>");
				c("<tr class='methods-header'  onclick='toggleView(this);'><th  colspan='2'><h2> Map </h2></th></tr>");
				c("</thead>");
				
			for (int i = 0; i < entrySet.getLength(); i++) {
				Node entry=entrySet.item(i);
				Node key= XMLUtil.extract("./key", entry).item(0);
				Node value= XMLUtil.extract("./value", entry).item(0);				
					c("<td>");
						emit(key, true, true);
					c("</td>");
					c("<td>");
						emit(value);
					c("</td>");
											
				c("</td>");
				c("</tr>");
			}						
			c("</tbody>");
			c("</table>");
			c("\n");
		}else{
			//for (int i = 0; i < nodeList.getLength(); i++) {
				c("<tr>");
				c("<td>  </td>");
				c("<td>");
					emit(selectedNode);
				c("</td>");
				c("</tr>");
			//}		
		}
		
		c("</td>");
		c("</tr>");	
		}//for
		
		}//for
		c("</tbody>");
		c("</table>");
		c("\n");
	}
	
	@Override
	protected  void emitMethods(NodeList nodeList) throws XPathExpressionException {
		c("<table class='methods' style='width:100%'>");
		c("<thead>");
		c("<tr class='methods-header'  onclick='toggleView(this);'><th  colspan='2'><h2> Methods </h2></th></tr>");
		c("</thead>");
		
		c("<tbody>");
		for (int i = 0; i < nodeList.getLength(); i++) {
			c("<tr>");
			Node node = nodeList.item(i);
			c("<td valign='top'>");
			c(node.getAttributes().getNamedItem("signature").getNodeValue());
			c("</td>");
			c("<td>");
			emitFields(XMLUtil.extract("field", node), true, true);
			c("</td>");
			c("</tr>");
		}
		c("</tbody>");
		c("</table>");
		c("\n");
	}
	
	@Override
	protected  void emitFields(NodeList nodeList, boolean skipHeader, boolean skipName) {
		c("<table class='fields' style='width:100%'>");
		if (!skipHeader){
			c("<thead>");
			c("<tr class='fields-header' onclick='toggleView(this);'><th colspan='3'><h2>Fields</h2></th></tr>");
			c("</thead>");
		}
		/*
		 * c("<tr style='width:100%' halign='left'>"); c("<th>type</th>");
		 * c("<th>name</th>"); c("<th>value</th>"); c("</tr>");
		 */
		c("<tbody>");
		for (int i = 0; i < nodeList.getLength(); i++) {
			c("<tr  style='width:100%'>");
			Node node = nodeList.item(i);
			c("<td  style='width:33%;white-space:nowrap;'>");
			c(node.getAttributes().getNamedItem("type").getNodeValue());
			c("</td>");
			if(!skipName){
				c("<td  style='width:33%;white-space:nowrap;'>");
				c(node.getAttributes().getNamedItem("name").getNodeValue());
				c("</td>");
			}
			c("<td style='width:33%;white-space:nowrap;'>");
			c(node.getAttributes().getNamedItem("value").getNodeValue());
			c("</td>");
			c("</tr>");
		}
		c("</tbody>");
		c("</table>");
	}

}
